﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using System.Web.Hosting;

namespace PaymentHelper.Core.ExceptionHandler
{
    public static class PaymentHelperLogger
    {
        private static readonly string _logFileConfig = @"~/App_Data/PaymentHelper.Log.config";
        private static ILog _logger;

        public static ILog GetLogger(string type)
        {
            if (LogManager.GetCurrentLoggers().Count() == 0)
            {
                LoadLogConfig();
            }

            return LogManager.GetLogger(type);
        }

        public static void Log(ExceptionLevel level, string type, object message)
        {
            _logger = GetLogger(type);
            if (_logger != null)
            {
                switch (level)
                {
                    case ExceptionLevel.Warning:
                        _logger.Warn(message);
                        break;
                    case ExceptionLevel.Error:
                        _logger.Error(message);
                        break;
                    case ExceptionLevel.Fatal:
                        _logger.Fatal(message);
                        break;
                }
            }
        }

        private static void LoadLogConfig()
        {
            XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(HostingEnvironment.MapPath(_logFileConfig)));
        }
    }
}
